package com.pfzy.utildemo.activity.websocket;

import android.os.Bundle;
import android.view.View;

import com.pfzy.utildemo.R;
import com.pfzy.utildemo.activity.base.BaseActivity;
import com.pfzy.lib.config.log.MyLog;
import com.pfzy.utildemo.databinding.ActivityWebsocketBinding;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.WebSocket;
import okhttp3.WebSocketListener;
import okio.ByteString;

/**
 * https://juejin.cn/post/7245179553887584312
 * https://juejin.cn/post/7244722310203621433
 * https://juejin.cn/post/6844903480998494215
 */
public class WebSocketActivity extends BaseActivity implements View.OnClickListener  {
    private ActivityWebsocketBinding mBind;
    private WebSocket webSocket;
    private final String TAG = "WebSocketDemo";
    private final int NORMAL_CLOSURE_STATUS = 1000;

    @Override
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        mBind = ActivityWebsocketBinding.inflate(getLayoutInflater());
        setContentView(mBind.getRoot());
        initView();
//        WsManager.openWs();
//        WsManager.register(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
//        WsManager.unregister(this);
    }

    private void initView() {
        OkHttpClient client = new OkHttpClient.Builder()
                .pingInterval(10, TimeUnit.SECONDS)// 设置 PING 帧发送间隔
                .build();
        Request request = new Request.Builder().url("wss://echo.websocket.org").build();

        webSocket = client.newWebSocket(request, new WebSocketListener() {
            @Override
            public void onClosed(@NotNull WebSocket webSocket, int code, @NotNull String reason) {
                super.onClosed(webSocket, code, reason);
                _log("关闭连接");
            }

            @Override
            public void onClosing(@NotNull WebSocket webSocket, int code, @NotNull String reason) {
                super.onClosing(webSocket, code, reason);
                _log("收到服务端发来的 close消息，准备关闭连接: " + reason);
            }

            @Override
            public void onFailure(@NotNull WebSocket webSocket, @NotNull Throwable t, @Nullable Response response) {
                super.onFailure(webSocket, t, response);
                _log("WebSocket 连接失败: " + t);
            }

            @Override
            public void onMessage(@NotNull WebSocket webSocket, @NotNull String text) {
                super.onMessage(webSocket, text);
                _log("收到消息: " + text);
//                webSocketListener.onMessageReceived(text);
            }

            @Override
            public void onMessage(@NotNull WebSocket webSocket, @NotNull ByteString bytes) {
                super.onMessage(webSocket, bytes);
                _log("收到消息: " + bytes.utf8());
            }

            @Override
            public void onOpen(@NotNull WebSocket webSocket, @NotNull Response response) {
                super.onOpen(webSocket, response);
                webSocket.send("WebSocket 链接成功");
            }
        });

        mBind.send.setOnClickListener(this);
        mBind.close.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.send:
                webSocket.send("1");
                break;
            case R.id.close:
                webSocket.close(NORMAL_CLOSURE_STATUS, "WebSocket connection closed");
                break;
            default:
                break;
        }
    }

    private void _log(String msg) {
        MyLog.logd("WebSocket: " + msg);
    }
}
